package com.tencent.bk.sdk.iam.dto.callback.response;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.tencent.bk.sdk.iam.dto.PathInfoDTO;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

@Data
@EqualsAndHashCode(callSuper=true)
public class InstanceInfoDTO extends BaseInfoDTO {
    @JsonProperty("_bk_iam_path_")
    @JsonSerialize(
            using = IamPathSerializer.class,
            as = List.class
    )
    private List<PathInfoDTO> path;

    static class IamPathSerializer extends JsonSerializer<List<PathInfoDTO>> {
        IamPathSerializer() {
        }

        public void serialize(List<PathInfoDTO> value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
            List<String> strList = (List)value.parallelStream().map(Object::toString).collect(Collectors.toList());
            gen.writeObject(strList);
        }
    }
}
